package stock;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * TODO 添加注释
 *
 * @author luotao
 * @version 1.0 2013-02-19
 */

public class MainFrame extends JPanel implements Runnable{

    private static final int SLEEP=5000;

    JLabel lebelName = new JLabel("名称");

    JLabel lebelNameValue = new JLabel("");

    JLabel lebelCurr = new JLabel("价格");

    JLabel lebelCurrValue = new JLabel("");

    private static final String url ="http://hq.sinajs.cn/list=sh601901";

    public MainFrame() {
        setLayout(new GridLayout(2, 4));
        add(lebelName);
        add(lebelNameValue);
        add(lebelCurr);
        add(lebelCurrValue);
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        while (true){
            try {
                Date date = new Date(System.currentTimeMillis());
                SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String s_date = fm.format(date);
                System.out.println("load info,current time : "+s_date);
                Http http = new Http();
                byte[] bb = http.doHttp(url,10,10,"GET","".getBytes());
                String result = new String(bb,"GBK");
                System.out.println("result = " + result);
                int index = result.indexOf("=");
                result=result.substring(index+2,result.length());
                String[] infos = result.split("\\,");
                String name = infos[0];
                String value = infos[3];
                lebelNameValue.setText(name);
                lebelCurrValue.setText(value);
                Thread.sleep(SLEEP);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        JFrame jFrame = new JFrame("stock");
        MainFrame mainFrame = new MainFrame();
        jFrame.getContentPane().add(mainFrame);
        jFrame.setSize(300, 200);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
